package core;

import java.util.Random;

public abstract class Element {
	/*
	 * Every element created, this is increased. It is used to keep track of the
	 * id of every Element object
	 */
	private static int identify = 0;
	/* The position of the element */
	private int pos;
	/* id of the instance of Element */
	private int id;
	/* Is it displayed or not */
	private boolean visible = false;

	public Element() {
		this.id = ++identify;
	}

	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
	}

	public int getId() {
		return id;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public void place(Room room) {
		intPlace(room);
	}

	/* Randomly place a mob in a free cell */
	/* TODO this gets stuck */
	private void intPlace(Room room) {
		Random rand = new Random();
		int index;
		boolean found = false;
		while (!found) {
			index = rand
					.nextInt((room.getLongSide() * room.getShortSide()) - 1);
			if (room.getElement(index / room.getLongSide(),
					index % room.getLongSide()) == null) {
				found = true;
				this.setPos(index);
				room.addElement(this);
			}
		}
	}

	@Override
	public String toString() {
		return "Element [pos=" + pos + ", id=" + id + "]";
	}
}
